﻿// Turtle Graphics
//===============================================================================
// Copyright 2009 Jason Hogg.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TurtleGraphics;
using System.IO;

namespace UnitTests
{
    /// <summary>
    /// Simple set of unit tests to test parsing logic. 
    /// </summary>
    [TestClass]
    public class SimpleParserTests
    {
        public SimpleParserTests()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void ParseMoveToStatement()
        {
            string logoProgram = "MoveTo 100,100";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }

        [TestMethod]
        public void ParseMoveStatement()
        {
            string logoProgram = "Move 100,100";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }

        [TestMethod]
        public void ParseDrawStatement()
        {
            string logoProgram = "Draw 100";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }

        [TestMethod]
        public void ParseRotateStatement()
        {
            string logoProgram = "Rotate 90";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }

        [TestMethod]
        public void ParseColorStatement()
        {
            string logoProgram = "Color Red";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }

        [TestMethod]
        public void ParseLoopStatement()
        {
            string logoProgram = "Loop 5 { Rotate 10 }";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }


        [TestMethod]
        public void ParseFunctionStatement()
        {
            string logoProgram = "Function MyDraw { Draw 100 } Rotate 10";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }

        [TestMethod]
        public void ParseCallStatement()
        {
            string logoProgram = "Function Draw { Draw 100 } Call Draw ";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }

        [TestMethod]
        [ExpectedException(typeof(ApplicationException), "Invalid input")]
        public void ParseInvalidStatement()
        {
            string logoProgram = "Blah";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }

        [TestMethod]
        [ExpectedException(typeof(ApplicationException), "Invalid input")]
        public void ParseInvalidDrawStatement()
        {
            string logoProgram = " Draw Blah";
            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram);
            tp.Parse();
        }

        [TestMethod]
        public void ParseTurtleGraphicFile()
        {
            // This test is the most comprehensive as it tests parsing of a fairly
            // complex program - the one that draws the US flag
            string logoProgramFileName = @"..\..\..\logo2\bin\debug\logosamples\USFlagWithFunctions.logo";
            StringBuilder logoProgram = new StringBuilder();

            using (StreamReader sr = new StreamReader(logoProgramFileName))
            {
                String line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    logoProgram.Append(line);
                    logoProgram.Append(Environment.NewLine);
                }
            }

            TurtleGraphicsParser tp = new TurtleGraphicsParser(@"..\..\..\logo2\bin\debug\Logo.mx", @"Languages.Logo", logoProgram.ToString());
            tp.Parse();
        }
    }
}
